<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // 这个map主要就是不让reject报错 及时有reject也会让promise.all正常接收到所有的值
      Promise.map = function (vals, cb) {
        return Promise.all(
          vals.map((val) => {
            return new Promise(function (resolve, reject) {
              console.log("map");
              cb(val, resolve);
            });
          })
        );
      };
      let p1 = Promise.resolve(21);
      let p2 = Promise.resolve(42);
      let p3 = Promise.reject("oops");
      Promise.map([p1, p2, p3], function (pr, done) {
        pr.then((res) => {
          done(res * 2);
        }).catch((err) => {
          done(err);
        });
      }).then((arr) => {
        console.log(arr);
      });
    </script>
  </body>
</html>
